package com.codinghero.example.cli;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;

public class CliLsExample {

	/**
	 * Usage: ls [OPTION]... [FILE]...
		List information about the FILEs (the current directory by default).
		Sort entries alphabetically if none of -cftuSUX nor --sort.
		
		-a, --all                  do not hide entries starting with .
		-A, --almost-all           do not list implied . and ..
		-b, --escape               print octal escapes for nongraphic characters
		    --block-size=SIZE      use SIZE-byte blocks
		-B, --ignore-backups       do not list implied entries ending with ~
		-c                         with -lt: sort by, and show, ctime (time of last
		                           modification of file status information)
		                           with -l: show ctime and sort by name
		                           otherwise: sort by ctime
		-C                         list entries by columns
	 * 
	 * @param args
	 * @return
	 */
	@SuppressWarnings("static-access")
	private static CommandLine parseArgs(String[] args) {
		// create the command line parser
		CommandLineParser parser = new PosixParser();

		// create the Options
		Options options = new Options();
		options.addOption( "a", "all", false, "do not hide entries starting with ." );
		options.addOption( "A", "almost-all", false, "do not list implied . and .." );
		options.addOption( "b", "escape", false, "print octal escapes for nongraphic "
		                                         + "characters" );
		options.addOption( OptionBuilder.withLongOpt( "block-size" )
		                                .withDescription( "use SIZE-byte blocks" )
		                                .hasArg()
		                                .withArgName("SIZE")
		                                .create() );
		options.addOption( "B", "ignore-backups", false, "do not list implied entried "
		                                                 + "ending with ~");
		options.addOption( "c", false, "with -lt: sort by, and show, ctime (time of last " 
		                               + "modification of file status information) with "
		                               + "-l:show ctime and sort by name otherwise: sort "
		                               + "by ctime" );
		options.addOption( "C", false, "list entries by columns" );

		try {
			return parser.parse(options, args);
		} catch (ParseException exp) {
			throw new RuntimeException("Unexpected exception:" + exp.getMessage());
		}
	}
	
	public static void main(String[] args) {
		CommandLine cmd = parseArgs(args);
		if (cmd.hasOption("block-size")) {
			System.out.println(cmd.getOptionValue("block-size"));
		}
	}

}
